from appium.webdriver.common.appiumby import AppiumBy as By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pages.base_page import BasePage


class CoursePage(BasePage):
    """课题页面"""
    activity = '.activity.member.MyCoursesAndColumnsActivity'
    # 数据
    search_code = 84
    # === 3. 元素定位器（集中管理，便于维护） ===
    my_class_locator = (By.ID, 'com.xiachufang:id/tab_widget_content_classroom')
    my_course_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("我的课程")')  # 我的课程
    search_locator = (By.XPATH, '//android.widget.EditText')  # 课程搜索
    fail_tips_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("没有搜索到相关课程哦")')
    book_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("书架")')
    find_ele_book_loctor = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("现在就去发现喜欢的专栏·电子书")')
    share_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().description("分享")')
    copy_url_lcoator = (
        By.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.xiachufang:id/item_share_icon").instance(5)')
    success_tips = (By.ANDROID_UIAUTOMATOR, '')
    vip_more_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("查看更多")')
    free_class_success_tips_loctor = (By.ID, 'com.xiachufang:id/navigation_bar_title_layout')

    def click_my_class(self):
        """点击我的课堂"""
        self.click(self.my_class_locator)

    def click_my_course(self):
        """点击我的课程"""
        self.click(self.my_course_locator)

    def input_search(self):
        """点击搜索框输入搜索内容"""
        self.send_keys(self.search_locator, "666")

    def click_search_btn(self):
        """点击小键盘搜索按钮"""
        self.press_key(self.search_code)

    def click_book(self):
        """点击书架"""
        self.click(self.book_locator)

    def click_find_ele_book(self):
        """点击发现电子书"""
        self.click(self.find_ele_book_loctor)

    def click_share(self):
        """点击分享"""
        self.click(self.share_locator)

    def get_copy_url_ele(self):
        """获取拷贝链接元素"""
        return self.find_element(self.copy_url_lcoator)

    def click_copy_url(self):
        """点击拷贝链接"""
        self.click(self.copy_url_lcoator)

    def click_vip_more(self):
        """更多"""
        self.click(self.vip_more_locator)

    def get_free_class_tips(self):
        """获取会员免费课"""
        return self.find_element(self.free_class_success_tips_loctor).text

    def get_fail_tips(self):
        """得到不存在课程失败的信息"""
        print("当前页面为：", self.driver.current_activity)
        self.switch_to_webview()
        fail_tips = self.wait_webview_element(self.fail_tips_locator).text
        self.switch_to_native()
        return fail_tips

    def get_toast(self, message="链接已复制到剪切板", timeout=5):
        """
        捕获 Toast 提示
        :param message: toast
        :param timeout: 超时时间
        :return: toast 文本
        """
        try:
            # Toast 通常用text属性
            locator = (By.XPATH, f"//*[contains(@text, '{message}')]")
            element = WebDriverWait(self.driver, timeout, 0.1).until(
                EC.presence_of_element_located(locator)
            )
            return element.get_attribute("text")
        except Exception:
            raise Exception("未捕获到 Toast")
